﻿using System;

namespace EasyIot.WinApp
{
    public partial class frmLogin : SystemForm
    { 
        TextBoxInput m_Curtxt;
        string m_CurTabIndex;

        public frmLogin()
        {
            InitializeComponent();

            DataMgr.SetDoubleBuffered(txtPhone);
            DataMgr.SetDoubleBuffered(txtPassword);
        }

        public override void ShowForm(RedirectType eType = RedirectType.Redirect_Next, object oParam = null)
        {
            base.ShowForm();
            ClearData();
            DataMgr.SetHorizontalCenter(this.Width, lblMsg, "...");
        }

        void ClearData()
        { 
            m_CurTabIndex = "1";
            m_Curtxt = txtPhone;
            txtPhone.SetTextBox(1, 11);
            txtPassword.SetTextBox(2, 4);
            txtPhone.SetCursor();  
        }

        public void OnTTKeyBoardInput(KeyType eKeyType, string strInput)
        { 

            strInput = strInput.Trim();

            if (eKeyType == KeyType.Key_TEXTBOX_FORCECHANGE)
            {
                AutoChangeForce(strInput);// 手动进行光标切换
            }
            else
            {
                // 按键信息
                switch (eKeyType)
                { 
                    case KeyType.Key_NUMBER:
                        // 自动进行光标切换
                        if ("1".Equals(m_CurTabIndex))
                        {
                            if (txtPhone.GetText().Trim().Length == 11)
                            {
                                m_CurTabIndex = "2";
                                m_Curtxt = txtPassword;// 验证通过自动跳转到下个文本框
                            }
                        }
                        m_Curtxt.InputText(strInput);
                        if ("2".Equals(m_CurTabIndex))
                        {
                            if ((txtPhone.GetText().Trim().Length == 11) && (txtPassword.GetText().Trim().Length == 4))
                            {
                                NextStep();// 验证通过自动调用下一步
                            }
                        }
                        break;
                    case KeyType.Key_SPACE:
                        m_Curtxt.InputText(strInput);
                        if ("2".Equals(m_CurTabIndex) && "".Equals(txtPassword.GetText()))
                        {
                            AutoChangeForce("1");// 焦点自动切换回上一个文本框
                        }
                        break;
                    case KeyType.Key_ENTER:
                        if ((txtPhone.GetText().Trim().Length == 11) && (txtPassword.GetText().Trim().Length == 4))
                        {
                            NextStep();// 验证通过自动调用下一步
                        }
                        break; 
                }
            }
        }

        void AutoChangeForce(string strInput)
        {
            m_CurTabIndex = strInput;

            switch (m_CurTabIndex)
            {
                case "1": m_Curtxt = txtPhone; break;
                case "2": m_Curtxt = txtPassword; break; 
            }

            m_Curtxt.SetCursor();
        }  

        void NextStep()
        {
            OnEventShowForm(FormCase.Form_Admin);
        } 

        private void btnBack_Click(object sender, EventArgs e)
        {
            OnEventShowForm(FormCase.Form_StandBy);
        }
    }
}
